<template>
	<view class="page">
		<cu-custom bgColor="bg-gradual-green" :isBack="true">
			<block slot="backText"></block>
		</cu-custom>
		<view class="padding">
			<view class="padding">
				<button class="cu-btn block bg-green margin-tb-sm lg" open-type="getUserInfo" @getuserinfo="mpGetUserInfo">微信登陆</button>
			</view>
		</view>
		<!-- 广告 -->
        <ad unit-id="adunit-acd4a47b558a507b"></ad>
	</view>
</template>

<script>
	import {
		mapActions
	} from 'vuex'
	export default {
		data() {
			return {
			}
		},
		onShow() {
			console.log("success")
		},
		onHide() {
			console.log("hehe")
		},
		onLoad() {
			console.log("haha")
		},
		methods: {
			...mapActions(['wxLogin']),
			mpGetUserInfo(result) {
				console.log('mpGetUserInfo', result);
				if (result.detail.errMsg !== 'getUserInfo:ok') {
					uni.showModal({
						title: '获取用户信息失败',
						content: '错误原因' + result.detail.errMsg,
						showCancel: false
					});
					return;
				}
				uni.showLoading({
					title: '登陆中...'
				});
				this.wxLogin(result.detail.userInfo).then(res=>{
                    uni.hideLoading();
                    uni.navigateBack({
                        delta:1
                    })
				}).catch(err=>{
                    uni.hideLoading();
                    uni.navigateBack({
                    	delta:1
                    })
                    console.log(err)
                })
                
				// uni.login({
				// 	provider: 'weixin',
				// 	success: (loginRes) => {
						
				// 		console.log(loginRes)
				// 		this.$http.post('wxMPLogin', {
				// 			code: loginRes.code
				// 		}).then(res => {
				// 			uni.setStorageSync('token', res.data.token);
				// 			console.log(result.detail.userInfo)
				// 			this.$http.post('editInfo',{
				// 				nickname:result.detail.userInfo.nickName,
				// 				avatar:result.detail.userInfo.avatarUrl
				// 			}).then(re=>{
				// 				console.log(re)
				// 				this.$http.post('getUser').then(r => {
				// 					console.log(r)
				// 					if(r.code == 1){
				// 						this.$store.state.userinfo = r.data;
				// 						uni.hideLoading();
				// 						uni.navigateBack({
				// 							delta:1
				// 						})
				// 					}
				// 				})
				// 			})
				// 		})
				// 	}
				// });
			},
		}
	}
</script>

<style scoped lang="less">
	.page{
		background-color: #fff;
	}
	button{
	}
</style>
